<template>
  <div class="page-header-index-wide">
    <a-row :gutter="24">
      <a-col :sm="32" :md="16" :xl="8" :style="{ marginBottom: '24px' }">
        <chart-card :loading="loading" title="近七日题目总数" :total="qstCount + ''">
          <a-tooltip title="近七日题目添加分布" slot="action">
            <a-icon type="info-circle-o" />
          </a-tooltip>
          <div>
            <mini-area :data-source="createTimeQstData" y="新增数量" />
          </div>
          <template slot="footer">今日添加数量  <span>{{todayQstCount}}</span></template>
        </chart-card>
      </a-col>
      <a-col :sm="32" :md="16" :xl="8" :style="{ marginBottom: '24px' }">
        <chart-card :loading="loading" title="近七日题型总数" :total="categoryCount + ''">
          <a-tooltip title="近七日题型添加分布" slot="action">
            <a-icon type="info-circle-o" />
          </a-tooltip>
          <div>
            <mini-area :data-source="categoryQstData" y="题目数量" />
          </div>
          <template slot="footer">最大题型数量  <span> {{ maxCategory }}</span></template>
        </chart-card>
      </a-col>
      <a-col :sm="32" :md="16" :xl="8" :style="{ marginBottom: '24px' }">
        <chart-card :loading="loading" title="近七日激活码总数" :total="cdkCount + ''">
          <a-tooltip title="近七日激活码添加分布" slot="action">
            <a-icon type="info-circle-o" />
          </a-tooltip>
          <div>
            <mini-bar :height="40" :data-source="createTimeCdkData" />
          </div>
          <template slot="footer">今日添加数量 <span>{{ todayCdkCount }}</span></template>
        </chart-card>
      </a-col>
    </a-row>

    <a-card :loading="loading" :bordered="false" :body-style="{padding: '0'}">
      <div class="salesCard">
        <a-tabs default-active-key="1" size="large" :tab-bar-style="{marginBottom: '24px', paddingLeft: '16px'}">
          <div class="extra-wrapper" slot="tabBarExtraContent">
            <div class="extra-item">
              <a @click="getSaleData('1')">近一周</a>
              <a @click="getSaleData('2')" >近一月</a>
              <a @click="getSaleData('3')">近一年</a>
            </div>
            <!-- <a-range-picker :style="{width: '256px'}" /> -->
          </div>
          <a-tab-pane loading="true" tab="实际销售额" key="1">
            <a-row>
              <a-col :xl="24" :lg="24" :md="24" :sm="24" :xs="24">
                <bar :dataSource="limit30RealData"/>
              </a-col>
            </a-row>
          </a-tab-pane>
          <a-tab-pane tab="预期销售额" key="2">
            <a-row>
              <a-col :xl="24" :lg="24" :md="24" :sm="24" :xs="24">
                <bar :dataSource="limit30Data"/>
              </a-col>
            </a-row>
          </a-tab-pane>
        </a-tabs>
      </div>
    </a-card>

    <a-row>
      <a-col :span="24">
        <a-card :loading="loading" :bordered="false" title="最近一周访问量统计" :style="{ marginTop: '24px' }">
          <a-row>
            <a-col :span="6">
              <head-info title="今日IP" :content="loginfo.todayIp"></head-info>
            </a-col>
            <a-col :span="2">
              <a-spin class='circle-cust'>
                <a-icon slot="indicator" type="environment" style="font-size: 24px"  />
              </a-spin>
            </a-col>
            <a-col :span="6">
              <head-info title="今日访问" :content="loginfo.todayVisitCount"></head-info>
            </a-col>
            <a-col :span="2">
              <a-spin class='circle-cust'>
                <a-icon slot="indicator" type="team" style="font-size: 24px"  />
              </a-spin>
            </a-col>
            <a-col :span="6">
              <head-info title="总访问量" :content="loginfo.totalVisitCount"></head-info>
            </a-col>
            <a-col :span="2">
              <a-spin class='circle-cust'>
                <a-icon slot="indicator" type="rise" style="font-size: 24px"  />
              </a-spin>
            </a-col>
          </a-row>
          <line-chart-multid :fields="visitFields" :dataSource="visitInfo"></line-chart-multid>
        </a-card>
      </a-col>
    </a-row>
  </div>
</template>

<script>
  import ChartCard from '@/components/ChartCard'
  import ACol from "ant-design-vue/es/grid/Col"
  import ATooltip from "ant-design-vue/es/tooltip/Tooltip"
  import MiniArea from '@/components/chart/MiniArea'
  import MiniBar from '@/components/chart/MiniBar'
  import MiniProgress from '@/components/chart/MiniProgress'
  import Bar from '@/components/chart/Bar'
  import LineChartMultid from '@/components/chart/LineChartMultid'
  import HeadInfo from '@/components/tools/HeadInfo.vue'

  import Trend from '@/components/Trend'
  import { getLoginfo,getVisitInfo } from '@/api/api'
  import { getAction } from '@/api/manage'
  import moment from 'moment'

  export default {
    name: "IndexChart",
    components: {
      ATooltip,
      ACol,
      ChartCard,
      MiniArea,
      MiniBar,
      MiniProgress,
      Bar,
      Trend,
      LineChartMultid,
      HeadInfo
    },
    data() {
      return {
        loading: true,
        center: null,
        loginfo:{},
        visitFields:['ip','visit'],
        visitInfo:[],
        indicator: <a-icon type="loading" style="font-size: 24px" spin />,
        categoryCount: 0,
        maxCategory: '',
        categoryQstData: [],
        qstCount: 0,
        todayQstCount: 0,
        createTimeQstData: [],
        cdkCount: 0,
        todayCdkCount: 0,
        createTimeCdkData: [],
        limit30Data: [],
        limit30RealData: [],
        url:{
          limit7groupByCategory: "/question/aoQuestion/limit7groupByCategory",
          limit7groupByCreateTime: "/question/aoQuestion/limit7groupByCreateTime",
          limit7groupByCreateTimeCdkey: "/cdkey/aoCdkey/limit7groupByCreateTime",
          limit30groupByCreateTimeCdkey: "/cdkey/aoCdkey/limit30groupByCreateTime",
          limitYearGroupByCreateTime: "/cdkey/aoCdkey/limitYearGroupByCreateTime",
        }
      }
    },
    created() {
      setTimeout(() => {
        this.loading = !this.loading
      }, 1000)
      this.initLogInfo();
      this.limit7groupByCategory()
      this.limit7groupByCreateTime()
      this.limit7groupByCreateTimeCdkey()
    },
    methods: {
      initLogInfo () {
        getLoginfo(null).then((res)=>{
          if(res.success){
            Object.keys(res.result).forEach(key=>{
              res.result[key] =res.result[key]+""
            })
            this.loginfo = res.result;
          }
        })
        getVisitInfo().then(res=>{
          if(res.success){
             this.visitInfo = res.result;
           }
         })
      },
      limit7groupByCategory(){
        getAction(this.url.limit7groupByCategory).then((res)=>{
          if( res.success ){
            let category = '0';
            let max = 0;
            for( let i = 0 ; i < res.result.length ; i ++ ){
              if( res.result[i].y > max ){
                category = res.result[i].x
                max = res.result[i].x
              }
            }
            this.maxCategory = category;
            this.categoryCount = res.result.length;
            this.categoryQstData = res.result;
          }
        })
      },
      limit7groupByCreateTime(){
        getAction(this.url.limit7groupByCreateTime).then((res)=>{
          if( res.success ){
            let sum = 0;
            for( let i = 0 ; i < res.result.length ; i ++ ){
              sum += res.result[i].y
              if( res.result[i].x === moment().format('yyyy-MM-dd') ){
                this.todayQstCount = res.result[i].y
              }
            }
            this.qstCount = sum;
            this.createTimeQstData = res.result;
          }
        })
      },
      limit7groupByCreateTimeCdkey(){
        getAction(this.url.limit7groupByCreateTimeCdkey).then((res)=>{
          if( res.success ){
            let sum = 0;
            for( let i = 0 ; i < res.result.length ; i ++ ){
              sum += res.result[i].y
              if( res.result[i].x === moment().format('yyyy-MM-dd') ){
                this.todayCdkCount = res.result[i].y
              }
            }
            this.cdkCount = sum;
            this.createTimeCdkData = res.result;
            this.getSaleData('1')
          }
        })
      },
      getSaleData(args){
        let url = this.url.limit30groupByCreateTimeCdkey
        if( args === '1' ){
          this.limit30Data = this.createTimeCdkData
          this.limit30RealData = this.createTimeCdkData.map(item=>{
            return {
              x: item.x,
              y: item.z
            }
          })
          return false;
        }else if( args === '3' ){
          url = this.url.limitYearGroupByCreateTime
        }
        getAction(url).then((res)=>{
          if( res.success ){
            this.limit30Data = res.result
            this.limit30RealData = res.result.map(item=>{
              return {
                x: item.x,
                y: item.z
              }
            })
          }
        })
      }
    }
  }
</script>

<style lang="less" scoped>
  .circle-cust{
    position: relative;
    top: 28px;
    left: -100%;
  }
  .extra-wrapper {
    line-height: 55px;
    padding-right: 24px;

    .extra-item {
      display: inline-block;
      margin-right: 24px;

      a {
        margin-left: 24px;
      }
    }
  }

  /* 首页访问量统计 */
  .head-info {
    position: relative;
    text-align: left;
    padding: 0 32px 0 0;
    min-width: 125px;

    &.center {
      text-align: center;
      padding: 0 32px;
    }

    span {
      color: rgba(0, 0, 0, .45);
      display: inline-block;
      font-size: .95rem;
      line-height: 42px;
      margin-bottom: 4px;
    }
    p {
      line-height: 42px;
      margin: 0;
      a {
        font-weight: 600;
        font-size: 1rem;
      }
    }
  }
</style>